<?php
/**
 * User:ayou chow
 * Date: 2019-12-09
 * Time: 15:52
 */

namespace app\admin\controller;


use app\common\model\UserLogModel;
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
use think\facade\Request;

class LogController extends BaseController
{
    public function Login()
    {
        return $this->fetch();
    }

    public function getlist()
    {
        $all   = $this->getAllParams();
        $page  = Request::param("page");
        $limit = Request::param("limit");

        $where = [];

        if (isset($all['uid']) && is_numeric($all['uid'])) {
            $where[] = "uid={$all["uid"]}";
        }
        if (isset($all['ip']) && $all['ip']) {
            $where[] = "last_ip like '%{$all["ip"]}%' OR device like '%{$all["ip"]}%'";
        }
        if (isset($all['start_time']) && $all['start_time']) {
            $where[] = "create_time >= {$all["start_time"]}";
        }
        if (isset($all['end_time']) && $all['end_time']) {
            $where[] = "create_time <= {$all["start_time"]}";
        }
        $where = implode(' AND ', $where);

        $list = UserLogModel::where($where)->order("id desc")->page($page, $limit)->select();
        if (count($list)) {
            $count = UserLogModel::where($where)->count("id");
            return json(["code" => 0, "msg" => "", "data" => $list, "count" => $count]);
        }

        return self::bulidFail("未查询到相关数据");
    }

    public function excel()
    {
        $all = $this->getAllParams();

        $where = [];

        if (isset($all['uid']) && is_numeric($all['uid'])) {
            $where[] = "uid={$all["uid"]}";
        }
        if (isset($all['ip']) && $all['ip']) {
            $where[] = "last_ip like '%{$all["ip"]}%' OR device like '%{$all["ip"]}%'";
        }
        if (isset($all['start_time']) && $all['start_time']) {
            $where[] = "create_time >= {$all["start_time"]}";
        }
        if (isset($all['end_time']) && $all['end_time']) {
            $where[] = "create_time <= {$all["start_time"]}";
        }
        $where = implode(' AND ', $where);

        $log_model = UserLogModel::where($where);

        $type_arr = [
            1 => '正常登录',
            2 => 'IP黑名单登录',
            3 => '设备黑名单登录',
        ];

        $list = [];
        $log_model->chunk(1000, function ($querys) use (&$list, $type_arr) {
            foreach ($querys as $query) {
                $list[] = [
                    $query->uid,
                    $query->user_type == 2 ? '主播' : '用户',
                    $query->group,
                    $query->device,
                    $query->last_ip,
                    $query->ip_location,
                    $query->create_time,
                    // $query->danger_level == 2 ? '风险登录' : '正常登陆',
                    // isset($type_arr[$query->type]) ? $type_arr[$query->type] : $type_arr[1],
                ];
            }
        }, 'id', 'desc');

        $file_title = ['会员账号', '账号属性', '所属公会', '设备号', '登录IP', 'IP所属地址', '登录日期'];
        return export_excel($list, $file_title, '会员登录日志' . date('Y年m月d日H点i分'), '会员登录日志');
    }
}